'use strict';
/**
 * JQGrid-service
 * 
 * @author
 * @version  [1.0.0, 2017/10/23]
 * @see
 * @since 1.0.0
 */
app.service('JQGridService',
    ['$q', '$resource', '$http', 'CrudService', 'DynamicI18n', 'Constant', 'dialogs',
     JQGridService]);

	function JQGridService($q, $resource, $http, CrudService, DynamicI18n, Constant, dialogs){
		var that = this;
		
		// 常量声明
	    var STATUS = {
	        CREATE: {name: '待提交', value: 1, color: 'primary'},
	        COMMIT: {name: '待审批', value: 2, color: 'info'},
	        THROUGH: {name: '已通过', value: 3, color: 'success'},
	        REFUSE: {name: '已打回', value: 4, color: 'danger'}
	    };
		
		that.getJQGrid = function(){
			var _gridWidth = 1024;
            var _columnWidth = _gridWidth / 7;
			var JQGrid = {   
			    url: ISS.getPath('/demo/components/table/jqgrid/table-jqgrid.json'),   
			    datatype:"json", //为local时初始化不加载，支持json，xml等   
			    mtype: "POST", 
			    altRows: true,
			    styleUI: 'Bootstrap',
			    altclass: "ui-jqgrid",
			    colNames:['id',
			              getMessage('demo.attribute1'),
			              getMessage('demo.attribute2'),
			              getMessage('demo.attribute3'),
			              getMessage('demo.status'),
			              getMessage('common.operation')], //表头   
			    colModel:[ //这里会根据index去解析jsonReader中root对象的属性，填充cell   
			        {name:'id', hidden: true},  
			        {name:'attribute1', index:'attribute1', width: _columnWidth, align:"center"},   
			        {name:'attribute2', index:'attribute2', width: _columnWidth, align:"center"},   
			        {name:'attribute3', index:'attribute3', width: _columnWidth, align:"center"},   
			        {name:'status', index:'status', width: _columnWidth, align:"center",
			        	formatter: function(cellvalue, options, rowdata){
				        	for (var key in STATUS) {
	                            var status = STATUS[key];
	                            if (status.value === cellvalue)
	                                return status.name;
	                        }
	                        return '未知状态';
			        }},
			        {name:'common.operation', width: _columnWidth * 2, edittype: "button", align:"center", sortable:false,
			        	formatter: function(cellvalue, options, rowdata){
			        		var html = [];
	                        //操作
	                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.operation(' + rowdata.id + ')">');
	                        html.push('<i class="fa fa-asterisk"></i>');
	                        html.push('<span translate="common.operation">操作</span>');
	                        html.push('</button>');
	                        //修改
	                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.edit(' + rowdata.id + ')">');
	                        html.push('<i class="fa fa-pencil"></i>');
	                        html.push('<span translate="common.edit">修改</span>');
	                        html.push('</button>');
	                        //查看
	                        html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.read(' + rowdata.id + ')">');
	                        html.push('<i class="fa fa-search"></i>');
	                        html.push('<span translate="common.read">查看</span>');
	                        html.push('</button>');
	                        //删除
	                        html.push('<button class="btn m-r-xs btn-xs btn-danger" ng-click="vm.del(' + rowdata.id + ')">');
	                        html.push('<i class="fa fa-trash-o"></i>');
	                        html.push('<span translate="common.del">删除</span>');
	                        html.push('</button>');
	                        
	                        return html.join('');
			        	}}
			    ],  
			    autowidth: true,
			    width: '100%', //数字 & 'auto','100%'   
			    height: 'auto', //200,   
			    rowNum: 5, //每页记录数   
			    rowList:[5, 10, 20, 30], //每页记录数可选列表   
			    pager: '#pager', //分页标签divID   
			    viewrecords: true, //显示记录数信息，如果这里设置为false,下面的不会显示 recordtext: "第{0}到{1}条, 共{2}条记录", //默认显示为{0}-{1} 共{2}条 scroll: false, //滚动翻页，设置为true时翻页栏将不显示  
			    /**这里是排序的默认设置，这些值会根据列表header点击排序时覆盖*/ 
			    sortable: false,   
			    sortname: "id",   
			    sortorder: "desc",   
//			    caption:"demo", //显示查询结果表格标题   
			    rownumbers: true, //设置列表显示序号,需要注意在colModel中不能使用rn作为index   
//			    rownumWidth: 20, //设置显示序号的宽度，默认为25   
			    multiselect: true, //多选框   
			    multiboxonly: true, //在点击表格row时只支持单选，只有当点击checkbox时才多选，需要multiselect=true是有效   
			    prmNames : { //如当前查询实体为demo，这些可以在查询对象的superObject中设定   
			        page: "demo.page",   
			        rows: "demo.rows",   
			        sort: "demo.id",   
			        order: "demo.id",   
			        search: "demo.search"   
			    },   
			    jsonReader:{ //server返回Json解析设定  ，对应json中数据列表   
			        root: "resultLlist", // json中代表实际模型数据的入口
			        page: "page",   // json中代表当前页码的数据
			        total: "totalPage",   // json中代表页码总数的数据
			        records: "totalCount",  // json中代表数据行总数的数据
			        repeatitems: true,   
			    }   
			}; 
			
			return JQGrid;
		}
		
		
		
	}

